
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.PublishItems;
import com.crv.ci.service.IPublishItemsService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class PublishItemsAction extends AbstractAdminController<IPublishItemsService>{

    @Autowired
    private IPublishItemsService publishItemsService;
    
    @RequestMapping(value="/publishitems/list")
    public String list(PublishItems publishItems,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(publishItems); 	
    	BizData4Page<PublishItems> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("publishItems", publishItems);
    	return "/module/publishitems/list";
    }    
    
    @RequestMapping(value="/publishitems/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("publishItems", new PublishItems());
        return "module/publishitems/edit";
    }    
    
    @RequestMapping(value="/publishitems/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	PublishItems publishItems = publishItemsService.fetch(id);
    	model.addAttribute("publishItems", publishItems);
        return "module/publishitems/edit";
    }    
    
    @RequestMapping(value="/publishitems/save")
    @ResponseBody
    public String save(HttpServletRequest request,PublishItems publishItems){
		boolean isAddNew = StringUtils.isBlank(publishItems.getId())?true:false;
		if(isAddNew)
		{
			publishItems.setId(IdGenerator.createNewId());
			publishItemsService.add(publishItems);
		}
		else
		{
			publishItemsService.update(publishItems);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/publishitems/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	publishItemsService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(PublishItems publishItems)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("publishId", publishItems.getPublishId());		
		conditions.put("clientIp", publishItems.getClientIp());		
		conditions.put("agentId", publishItems.getAgentId());		
		conditions.put("status", publishItems.getStatus());		
		conditions.put("returnMsg", publishItems.getReturnMsg());		
		conditions.put("returnMsg2", publishItems.getReturnMsg2());		
		conditions.put("errorMsg", publishItems.getErrorMsg());		
		conditions.put("startDate", publishItems.getStartDate());		
		conditions.put("endDate", publishItems.getEndDate());		
		conditions.put("publishDate", publishItems.getPublishDate());		
		conditions.put("createDate", publishItems.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IPublishItemsService getMainService() {
        return publishItemsService;
    }

    @Override
    protected String getMainObjName() {
        return "publishitems";
    }

    @Override
    protected String getViewTitle() {
        return "publishitems";
    }
}
